package com.KKd.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author 14587
 * @date 2020/11/4 16:50
 */
@Controller
public class Controller1 {
    //这样可以传入两个参数，但是请求中的a和b不能改变，名字是不能变的
    //原来的风格：http://localhost:8090/spring_03/add?a=1&b=2
    //RestFull风格：http://localhost:8090/spring_03/add/1/2
    /*@RequestMapping("/add{a}{b}")
    public String test(int a, int b, Model model){
        int result = a+b;
        model.addAttribute("msg","结果为"+result);
        return "hello";
    }*/

    //用RestFull风格下面的这种格式不能改变，这样可以定义方法
    /*@RequestMapping(value = "/add/{a}/{b}",method = RequestMethod.GET)
    public String test(@PathVariable int a, @PathVariable int b, Model model){
        model.addAttribute("msg","结果为"+a+b);
        return "hello";
    }*/
    @RequestMapping("/a")
    public String in(){
        return "a";
    }
    //下面的直接定义Mapping的方法也可以完成
    @GetMapping("/add/{a}/{b}")
    public String test2(@PathVariable int a,@PathVariable int b ,Model model){
        int result=a+b;
        model.addAttribute("msg","使用get方法，结果为"+result);
        return "redirect:/WEB-INF/jsp/a.jsp";
        //return "forward:/WEB-INF/jsp/a.jsp";
    }

    @PostMapping("/add/{a}/{b}")
    public String test3(@PathVariable int a,@PathVariable int b ,Model model){
        int result=a+b;
        model.addAttribute("msg","使用post方法，结果为"+result);
        return "a";
    }
}
